package pl.nazaweb.imedproplus.rest;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import pl.nazaweb.imedproplus.business.service.OrganizationStructureService;
import pl.nazaweb.imedproplus.model.entity.OrganizationStructure;
import pl.nazaweb.imedproplus.rest.pojo.AllowAddToStrucuturePojo;
import pl.nazaweb.imedproplus.rest.pojo.OrganizationStructureTreePojo;
import pl.nazaweb.imedproplus.rest.pojo.ResponsePojo;

/**
 *
 * @author naza
 */
@Stateless
@Path("/json/organizationstructure/tree")
public class OrganizationStructureTree {

    @EJB
    private OrganizationStructureService organizationStructureService;

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public List<OrganizationStructureTreePojo> getOrganizationStrucutreTree(@QueryParam("node") String parentId) {
        if (parentId.toLowerCase().equals("nan")) {
            parentId = "0";
        }
        List<OrganizationStructure> childs = this.organizationStructureService.getChildsByParent(new BigInteger(parentId));
        List<OrganizationStructureTreePojo> result = new ArrayList<OrganizationStructureTreePojo>();
        for (OrganizationStructure child : childs) {
            OrganizationStructureTreePojo childPojo = new OrganizationStructureTreePojo();
            childPojo.setElement(child);
            if (child.getHasChild()) {
                childPojo.leaf = false;
                childPojo.expandable = true;
            }
            result.add(childPojo);
        }
        return result;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/allowadd/get")
    public ResponsePojo allowAddChildToParent(@QueryParam("parentId") Long parentId, @QueryParam("childId") Long childId) {
        ResponsePojo response = new ResponsePojo();
        try {
            //TODO:
            //aktualnie sprawdzane jest tylko czy wystepuje w strukturze
            AllowAddToStrucuturePojo result = new AllowAddToStrucuturePojo();
            result.allowAddElementToHierarchy =
                    this.organizationStructureService.allowAddOrganizationStructureToTree(childId);
            response.data = result;
        } catch (Exception ex) {
            response.message = ex.getMessage();
            response.success = false;
        }
        return response;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/add")
    public ResponsePojo addChildToParent(OrganizationStructureTreePojo pojo) {
        ResponsePojo response = new ResponsePojo();
        try {
            this.organizationStructureService.addOrganizationStructureToStructureTree(pojo.parentId, pojo.id);
        } catch (Exception ex) {
            response.message = ex.getMessage();
            response.success = false;
        }
        return response;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/destroy")
    public ResponsePojo removeOrganizationStrucutreFromTree(OrganizationStructureTreePojo pojo) {
        ResponsePojo response = new ResponsePojo();
        try {
            if (!pojo.id.equals(new Long(-1))) {
                this.organizationStructureService
                        .removeOrganizationStructureFromTree(pojo.id, pojo.parentId);
            } else {
                response.message = "Nie można usunąć elemntu głównego.";
                response.success = false;
            }

        } catch (Exception ex) {
            response.message = ex.getMessage();
            response.success = false;
        }
        return response;
    }
}
